package game.frame;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;

public class CellPoint {
	
	private int x = 0;
	private int y = 0;
	
	private static int maxX;
	private static int maxY;
	
	private WorkPanelIfc wpIfc = null;
	private Color color = null;
	
	public CellPoint(int x, int y, Color color, WorkPanelIfc wpIfc) {
		this.wpIfc = wpIfc;
		this.color = color;
		correctCellPoint(x, y);
	}
	
	public CellPoint(int x, int y, WorkPanelIfc wpIfc) {
		this.wpIfc = wpIfc;
		this.color = new Color(255, 100, 100);
		correctCellPoint(x, y);
	}
	
	private void correctCellPoint(int x, int y) {
		x = correctX(x);
		y = correctY(y);
		
		this.x = x;
		this.y = y;
	}
	
	private int correctX(int x) {
		CellPoint.maxX = wpIfc.getWidth() / wpIfc.getCellSize() - 1;
		
		if (x > maxX) {
			x = maxX;
		}
		if (x < 0) {
			x = 0;
		}
		
		return x;
	}
	
	private int correctY(int y) {
		CellPoint.maxY = wpIfc.getHeight() / wpIfc.getCellSize() - 1;
		
		if (y > maxY) {
			y = maxY;
		}
		if (y < 0) {
			y = 0;
		}
		
		return y;
	}
	
	public CellPoint point2CellPoint(Point p, WorkPanel wp) {
		return new CellPoint(p.x, p.y, (WorkPanelIfc) wp);
	}
	
	public Point cellPoint2Point(CellPoint cp, WorkPanel wp) {
		this.wpIfc = (WorkPanelIfc) wp;
		
		return new Point(cp.x * wpIfc.getCellSize(),
							cp.y * wpIfc.getCellSize());
	}
	
	public void drawCellPoint(Graphics2D g, WorkPanelIfc wpIfc, boolean erase) {
		this.wpIfc = wpIfc;
		
		if(erase) {
			g.setColor(g.getBackground());
		}
		else {
			g.setColor(this.color);
		}
		
		int cellS = this.wpIfc.getCellSize();
		int x = this.x * cellS + 1;
		int y = this.y * cellS + 1;
//		cellS--;
		
		g.fillRect(x, y, cellS, cellS);
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public WorkPanelIfc getWpIfc() {
		return wpIfc;
	}

	public Color getColor() {
		return color;
	}

	public static int getMaxX() {
		return maxX;
	}

	public static int getMaxY() {
		return maxY;
	}

	public static void setMaxX(int maxX) {
		CellPoint.maxX = maxX;
	}

	public static void setMaxY(int maxY) {
		CellPoint.maxY = maxY;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public void setX(int x) {
		this.x = correctX(x);
	}

	public void setY(int y) {
		this.y = correctY(y);
	}

	public void setWpIfc(WorkPanelIfc wpIfc) {
		this.wpIfc = wpIfc;
	}

	/* 
	 * See java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "CellPoint [x = " + x + ", y = " + y + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((wpIfc == null) ? 0 : wpIfc.hashCode());
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CellPoint other = (CellPoint) obj;
		if (wpIfc == null) {
			if (other.wpIfc != null)
				return false;
		} else if (!wpIfc.equals(other.wpIfc))
			return false;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}
}
